package com.health.viewscontrols.focusgroup;

import java.util.ArrayList;

import com.health.viewscontrols.NovaFragmentActivity;
import com.health.viewscontrols.PersonalFileActivity.onFragmentSave;
import com.health.R;
import com.health.adapter.FragmentAdapter;
import com.health.fragment.focusgroup.JSJB_BaseFragment;
import com.health.fragment.focusgroup.JSJB_SFXXFragment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;

/**
 * 重点人群--精神疾病
 * @author nova
 *
 */
public class JSJBctivity extends NovaFragmentActivity implements
		OnClickListener {

	private ViewPager viewPager;
	private ArrayList<RadioButton> radioButtons;
	private ArrayList<Fragment> fragments;

	private TextView title;
	private Button btn_back;
	public static Button btn_up;
	FragmentAdapter adapter;
	public static Activity mJSJBctivity = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
//		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.jsjb_main);
		initView();
		addListener();
	}

	private void initView() {
		title=(TextView)findViewById(R.id.title);
	
		
		btn_back=(Button)findViewById(R.id.btn_back);
		btn_up=(Button)findViewById(R.id.btn_up);
		
		
		viewPager = (ViewPager) findViewById(R.id.viewpager);
		RadioButton maternity_base = (RadioButton) findViewById(R.id.sf_info_base);
		RadioButton maternity_sfxx = (RadioButton) findViewById(R.id.sf_info_sfxx);
		
		radioButtons = new ArrayList<RadioButton>();
		radioButtons.add(maternity_base);
		radioButtons.add(maternity_sfxx);
		maternity_base.setOnClickListener(this);
		maternity_sfxx.setOnClickListener(this);
		Fragment baseFragement = new JSJB_BaseFragment();
		Fragment sfxxFragement = new JSJB_SFXXFragment();
		fragments = new ArrayList<Fragment>();
		fragments.add(baseFragement);
		fragments.add(sfxxFragement);

		adapter= new FragmentAdapter(
				getSupportFragmentManager(), fragments);
		viewPager.setAdapter(adapter);
		title.setText("严重精神障碍患者  —— 基本信息");
		setChecked(0);
		mJSJBctivity = this;
	}

	private void addListener() {

		viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			@Override
			public void onPageScrolled(int position, float positionOffset,
					int positionOffsetPixels) {

			}

			@Override
			public void onPageSelected(int position) {
				radioButtons.get(position).setChecked(true);
				setChecked(position);
			}

			@Override
			public void onPageScrollStateChanged(int state) {

			}
		});
		
		btn_back.setOnClickListener(this);
		btn_up.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {

		int id = v.getId();
		if (id == R.id.sf_info_base) {
			viewPager.setCurrentItem(0);
			setChecked(0);
		} else if (id == R.id.sf_info_sfxx) {
			viewPager.setCurrentItem(1);
			setChecked(1);
		} else if (id == R.id.btn_back) {
			this.finish();
		} else if (id == R.id.btn_up) {
			//保存
			onFragmentSave currentFragment = (onFragmentSave) adapter.getCurrentFragment();
			currentFragment.save();
		} else {
		}
	
		
	}
	@SuppressLint("ResourceAsColor")
	private void setChecked(int pos) {
		for (RadioButton rbtn : radioButtons) {
			rbtn.setBackground(null);
		}
		radioButtons.get(pos).setBackgroundResource(R.drawable.rd_chek_center);
		if(pos==0){
			title.setText("严重精神障碍患者  —— 基本信息");
		}else if(pos==1){
			title.setText("严重精神障碍患者  —— 随访信息");
		}
	}
}
